package com.by.yz.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.util.ArrayList;
/**
 * Created by pengpeng on 2017/3/26.
 */
public class BigImageDetailActivity extends BaseActivity  implements View.OnTouchListener {
    private int mPosition,mLocationX,mLocationY,mWidth,mHeight;
    private ArrayList<String> mDatas = new ArrayList<String>();;
    private BigImageView imageView;
    GestureDetector.SimpleOnGestureListener myGestureListener;
    GestureDetector mGestureDetector;
    private static final int FLING_MIN_DISTANCE = 50;   //最小距离
    private static final int FLING_MIN_VELOCITY = 0;    //最小速度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        mDatas.clear();
        mDatas = (ArrayList<String>) getIntent().getSerializableExtra("images");
        mPosition = getIntent().getIntExtra("position", 0);
        mLocationX = getIntent().getIntExtra("locationX", 0);
        mLocationY = getIntent().getIntExtra("locationY", 0);
        mWidth = getIntent().getIntExtra("width", 0);
        mHeight = getIntent().getIntExtra("height", 0);

        setToolbarTitle((mPosition+1)+"/"+mDatas.size());

        imageView = new BigImageView(this);
        imageView.setOriginalInfo(mWidth, mHeight, mLocationX, mLocationY);
        imageView.transformIn();
        imageView.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        setContentView(imageView);
        //创建默认的ImageLoader配置参数
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .memoryCache(new UsingFreqLimitedMemoryCache(2*1024*1024))
                .threadPriority(Thread.MAX_PRIORITY)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.health_passage_default)//加载过程中显示的图片
                .showImageForEmptyUri(R.mipmap.empty_photo)//加载内容为空显示的图片
                .showImageOnFail(R.mipmap.health_topic_error)//加载失败显示的图片
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new FadeInBitmapDisplayer(388))
                .build();
        ImageLoader.getInstance().displayImage(mDatas.get(mPosition), imageView,options);

        myGestureListener = new GestureDetector.SimpleOnGestureListener(){
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.e("<--滑动测试-->", "开始滑动");
                float x = e1.getX()-e2.getX();
                float x2 = e2.getX()-e1.getX();
                int size = mDatas.size();
                if(x>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){// 向左手势
                    if(mPosition < size-1){
                        mPosition ++;
                    }else{
                        mPosition = 0;
                    }
                }else if(x2>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){// 向右手势
                    if(mPosition > 0){
                        mPosition --;
                    }else{
                        mPosition = size-1;
                    }
                }
                ImageLoader.getInstance().displayImage(mDatas.get(mPosition), imageView);
                setToolbarTitle((mPosition+1)+"/"+mDatas.size());
                // 返回true则截止后面的事件触发
                return true;
            }
        };
        mGestureDetector = new GestureDetector(this, myGestureListener);
        imageView.setOnTouchListener(this);//将主容器的监听交给本activity，本activity再交给mGestureDetector
        imageView.setLongClickable(true);  //必需设置这为true 否则也监听不到手势
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BigImageDetailActivity.this.finish();
                Log.e("<--点击测试-->", "开始点击");
            }
        });
    }

    //实现OnTouch方法 并将处理touch事件交给mGestureDetector
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return mGestureDetector.onTouchEvent(event);
    }
}
